/**
 * @copyright SilverBiology, LLC
 * @author SilverBiology, LLC
 * @website http://www.silverbiology.com
*/
Ext.ns("BioBlitz");

BioBlitz.FusionMapPanel = function(config){
	
	var position = new google.maps.LatLng( config.lat, config.lon );
		
	var mapOptions = {
			center: position
		,	zoom: config.zoom
		,	mapTypeId: google.maps.MapTypeId.TERRAIN
	}
				  
	this.map = new Ext.Map({
			mapOptions: mapOptions
		,	id:'fusionidmap'	
	});			  
	
	Ext.apply(this, config, { 
			items: [ this.map ]
		,	iconCls: 'search'
//		,	listeners:{
//				activate: this.addLayerfusionid
//			}
		,	dockedItems: [{
					xtype: 'toolbar'
				,	dock: 'top'
				,	title: config.title
				,	items: [{
						xtype:'spacer'
					}, {
							text: 'Refresh'
						,	ui: 'action'
						,	id: 'fmrefresh'
						,	iconCls: 'refresh'
						,	handler: function() {
								var zoomLevel = Ext.getCmp('fusionidmap').map.getZoom();
								Ext.getCmp('fusionidmap').map.setZoom(zoomLevel * 1);
							}	
					}]
			}]	
	});
		
	BioBlitz.FusionMapPanel.superclass.constructor.call(this,config);

};

Ext.extend(BioBlitz.FusionMapPanel, Ext.Panel, {
		
		addFusionLayer: function( fusion_id ){
//console.log( this, fusion_id );
			if (fusion_id) {
				if(!Ext.isEmpty(this.fusionidlayer)){
					if(this.fusionidlayer.tableId == fusion_id) {
						return;
					}
				}

				this.fusionLayer = new google.maps.FusionTablesLayer( fusion_id );
				this.fusionLayer.setMap( this.map );

				var zoomLevel = Ext.getCmp('fusionidmap').map.getZoom();
				Ext.getCmp('fusionidmap').map.setZoom(zoomLevel * 1);
			} else {
				if (!(this.fusionidlayer)) {
					alert(" No Fusion ID Available! ");
				}	else {
					this.fusionidlayer.setMap(null);
					alert(" No Fusion ID Available! ");
				}								
			}
	}

});